package com.gem.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 刘聪
 * Created  on 2020/10/9
 */
public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        System.out.println(uri);
        if (uri.endsWith("/login") || uri.endsWith("/logout") || uri.endsWith("/") || uri.endsWith("tologin") || uri.endsWith("js") || uri.endsWith("/style.css")) {
            //登录请求 放行
            return true;
        }
        HttpSession session = request.getSession();
        Object user = session.getAttribute("login");
        System.out.println("User:"+user);
        if (user != null) {
            return true;
        } else {
            response.sendRedirect(request.getContextPath() + "/tologin");
        }

//        response.sendRedirect(request.getContextPath() + "/tologin");
        return true;

    }
}
